package com.hz.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户没有登录时返回给前端的数据
 */
@Component
public class AjaxAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {


        String flagName = httpServletRequest.getAttribute("flagName").toString();
        System.out.println("flagName======"+flagName);

        MassageJson massageJson = new MassageJson();

        if (flagName.equals("未登录")){

            massageJson.setCode(001);
            massageJson.setMsg("未登录");
        } else if (flagName.equals("权限不足")){

            massageJson.setCode(002);
            massageJson.setMsg("权限不足");
        } else{
            massageJson.setCode(003);
            massageJson.setMsg("系统异常");
        };
        httpServletResponse.setContentType("text/html;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(massageJson));

    }
}
